// pages/MaintainHistoryDetails/MaintainHistoryDetails.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
var regeneratorRuntime = require('../../utils/runtime.js');
var utils = require('../../utils/util')
const CryptoJS = require('../../utils/aes_util.js') //参数加密
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"详情",
    memtype:"1"  ,   // 1  责任班组  2  外委单位
    TaskInfo:{},
    UploadUrl: httpRequest.uploadUrl,
    TaskState: null, //工单状态
    error: '', //错误提示
    info: '', //成功提示
    scrollHeight: 0, //滚动条高度
    TaskMian:[],
    TaskMianName:'',
  },


  MaintainHistoryDetails: async function () {
    var that = this;
    var Param = {
      TaskGuid: that.data.TaskGuid,
    }
    console.log("TaskGuid" + JSON.stringify(Param))
    let data = await utils.get(httpRequest.apiInfo.GetMaintenanceDetailsApi + "?" + CryptoJS.AesEncrypt(Param))
    console.log("MaintainHistoryDetails" + JSON.stringify(data))
    if (!utils.isEmptyObject(data.data.TaskInfo)) {
      this.setData({
        TaskInfo: data.data.TaskInfo,
        TaskItem: data.data.TaskInfo.TaskItem,
        TaskMian: data.data.TaskInfo.TaskMian
      })
    }
    if (that.data.TaskMian.length > 0){
      for (var i = 0; i < that.data.TaskMian.length; i++){
        that.setData({
          TaskMianName: that.data.TaskMian[i].DisplayName
        })
      }
    }
  },

  priviewimage:  function (e) {
    let index = e.currentTarget.dataset.index;
    let current = e.currentTarget.dataset.src;
    let imgarr = (this.data.TaskItem)[index].AttchInfo;
    let imglist = []
    for (let item of imgarr) {
      imglist.push(this.data.UploadUrl+item.FilePath)
    }
    console.log(imglist)

    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: imglist // 需要预览的图片http链接列表
    })
  }, 

  //审核通过
  pass:function(){
    var that = this;
    var param = {
      RowGuid: that.data.TaskGuid,
      TaskState:"1"
    }
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetDelegateExamineApi, param,
      function (data) {
        console.log(data)
        if (data == 0){
          that.setData({
            info:'审核通过成功'
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          },1000)
        }else{
          that.setData({
            error: '审核通过失败'
          })
        }
      },
      function (err) { })
  },

  //审核不通过
  Nopass: function () {
    var that = this;
    var param = {
      RowGuid: that.data.TaskGuid,
      TaskState: "4"
    }
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetDelegateExamineApi, param,
      function (data) {
        console.log(data)
        if (data == 0) {
          that.setData({
            info: '审核不通过成功'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        } else {
          that.setData({
            error: '审核不通过失败'
          })
        }
      },
      function (err) { })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let TaskGuid = options.TaskGuid;
    let EquipmentName = options.EquipmentName;
    let RecordBeginTime = options.RecordBeginTime;
    let TaskCompleteTime = options.TaskCompleteTime;
    let EquipmentLocation = options.EquipmentLocation;
    // let recordpeople = wx.getStorageSync("UserInfo").AppLogin.DisplayName;
    let TaskBeginTime = options.TaskBeginTime;
    let TaskEndTime = options.TaskEndTime;
    let TaskState = options.TaskState;

  
    if (RecordBeginTime == "null"){
      RecordBeginTime = ''
    }else{
      // RecordBeginTime = dateutil.dateFormat(RecordBeginTime)
      RecordBeginTime = RecordBeginTime.replace(/T/," ")
    }
    if (TaskCompleteTime == "null") {
      TaskCompleteTime = ''
    } else{
      // TaskCompleteTime = dateutil.dateFormat(TaskCompleteTime)
      TaskCompleteTime = TaskCompleteTime.replace(/T/, " ")
    }

    TaskBeginTime = dateutil.dateFormat(TaskBeginTime)
    TaskEndTime = dateutil.dateFormat(TaskEndTime)

    this.setData({
      TaskGuid: TaskGuid,
      EquipmentName: EquipmentName,
      RecordBeginTime: RecordBeginTime,
      TaskCompleteTime: TaskCompleteTime,
      EquipmentLocation: EquipmentLocation,
      // recordpeople: recordpeople,
      TaskBeginTime: TaskBeginTime,
      TaskEndTime: TaskEndTime,
      TaskState: TaskState
    })

    if (this.data.TaskState == 3){
      this.setData({
        scrollHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 90
      })
    }else{
      this.setData({
        scrollHeight: (app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2
      })
    }
    
    this.MaintainHistoryDetails()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})